#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/9/6 11:11
# @File           : __init__
# @IDE            : PyCharm
# @desc           : 描述

from fastapi import APIRouter, Depends
from utils.response import SuccessResponse
from . import schemas, crud, params
from core.dependencies import IdList
from apps.base.auth.validate import AllUserAuth, Auth


app = APIRouter()


@app.get("/task", summary="获取QAR任务")
async def get_task(
        p: params.QARTaskParams = Depends(),
        auth: Auth = Depends(AllUserAuth())
):
    datas, count = await crud.QARTaskDal(auth.db).get_datas(
        **p.dict(),
        v_schema=schemas.QARTaskSimpleOut,
        v_return_count=True
    )
    return SuccessResponse(datas, count=count)


@app.post("/task", summary="创建QAR任务")
async def create_task(
        data: schemas.QARTask,
        auth: Auth = Depends(AllUserAuth())
):
    data.create_user = auth.user.user_str
    return SuccessResponse(await crud.QARTaskDal(auth.db).create_data(data))


@app.put("/task/{data_id}", summary="更新QAR任务")
async def put_task(
        data_id: int,
        data: schemas.QARTask,
        auth: Auth = Depends(AllUserAuth())

):
    data.update_user = auth.user.user_str
    return SuccessResponse(await crud.QARTaskDal(auth.db).put_data(data_id, data=data))


@app.delete("/task", summary="批量删除QAR任务", description="硬删除")
async def delete_task(ids: IdList = Depends(), auth: Auth = Depends(AllUserAuth())):
    await crud.QARTaskDal(auth.db).delete_datas(ids=ids.ids, v_soft=False)
    return SuccessResponse("删除成功")
